package com.sunrise.generico.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.faces.context.FacesContext;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.ServletContext;

public class EmailUtil {
	
	public static void sendEmailUsingTemplate(String to, String email, String subject) throws MessagingException {
		
		FacesContext facesContext = FacesContext.getCurrentInstance();
		facesContext.responseComplete();
		ServletContext scontext = (ServletContext) facesContext.getExternalContext().getContext();
		
		
		boolean debug = false;
		// Set the host smtp address
		Properties props = new Properties();
		props.put("mail.smtp.host", ConfigUtil.getProperty("smtpServer"));
		props.put("mail.smtp.port", ConfigUtil.getProperty("smtpPort"));
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable","true"); 
		
		Authenticator auth = new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication()
		    {
		        String username = ConfigUtil.getProperty("username");
		        String password = ConfigUtil.getProperty("passwordMail");
		        return new PasswordAuthentication(username, password);
		    }
		};
		
		// create some properties and get the default Session
		Session session = Session.getDefaultInstance(props, auth);
		session.setDebug(debug);
		
		// Create the message
		Message message = new MimeMessage(session);

		// Fill its headers
		message.setSubject(subject);
		message.setFrom(new InternetAddress(ConfigUtil.getProperty("username")));
		
		InternetAddress addressTo = new InternetAddress(to);
		message.setRecipient(Message.RecipientType.TO, addressTo);

		// Create your new message part
		BodyPart messageBodyPart = new MimeBodyPart();
		messageBodyPart.setContent(email, "text/html");

		// Create a related multi-part to combine the parts
		MimeMultipart multipart = new MimeMultipart("related");
		multipart.addBodyPart(messageBodyPart);
		
		// Create part for the image
		messageBodyPart = new MimeBodyPart();

		DataSource logomarca = new FileDataSource(scontext.getRealPath("/paginas/recursos/imagens/layout/logomarca-white.png"));
		messageBodyPart.setDataHandler(new DataHandler(logomarca));
		messageBodyPart.setHeader("Content-ID","<logo>");
		multipart.addBodyPart(messageBodyPart);
		
		messageBodyPart = new MimeBodyPart();
		// Fetch the image and associate to part
		DataSource background = new FileDataSource(scontext.getRealPath("/paginas/recursos/imagens/layout/background.jpg"));
		messageBodyPart.setDataHandler(new DataHandler(background));
		messageBodyPart.setHeader("Content-ID","<background>");
		multipart.addBodyPart(messageBodyPart);
		
		messageBodyPart = new MimeBodyPart();
		DataSource backtop = new FileDataSource(scontext.getRealPath("/paginas/recursos/imagens/layout/back-top.gif"));
		messageBodyPart.setDataHandler(new DataHandler(backtop));
		messageBodyPart.setHeader("Content-ID","<top>");
		multipart.addBodyPart(messageBodyPart);
		
		messageBodyPart = new MimeBodyPart();
		DataSource footer = new FileDataSource(scontext.getRealPath("/paginas/recursos/imagens/layout/footer.gif"));
		messageBodyPart.setDataHandler(new DataHandler(footer));
		messageBodyPart.setHeader("Content-ID","<footer>");
		multipart.addBodyPart(messageBodyPart);

		// Associate multi-part with message
		message.setContent(multipart);

		Transport.send(message);
		
	}
	
	@SuppressWarnings("resource")
	public static String htmlEnviarSenha() {
		FileReader fr;
		try {
			FacesContext facesContext = FacesContext.getCurrentInstance();
			facesContext.responseComplete();
			ServletContext scontext = (ServletContext) facesContext.getExternalContext().getContext();
			fr = new FileReader(scontext.getRealPath("/paginas/emails/EmailTemplate.html"));
			BufferedReader br = new BufferedReader(fr);
			StringBuilder content = new StringBuilder(1024);
			String s = "";
			while ((s = br.readLine()) != null) {
				content.append(s);
			}
			return content.toString();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";

	}
	
}
